//读取、加载列表
var UrlTable = DataURL + 'bank/activity/getActivityByDeptId?checkStatus=';
var page = checkpage();
function checkpage() {
	var checkpage = $(".maincontent-table .control-card .control-mode.active");
	if (checkpage.hasClass("ToReviewing")) {
		return 1;
	}
	if (checkpage.hasClass("ToNotApproval")) {
		return 3;
	}
}
choseData(page);
function choseData(page) {
	if(page==1){loadreview()}
	if(page==3){loadnotapproval()}
}

function loadreview() {
	$.ajax({
		type : "GET",
		url : UrlTable + "1",
		dataType : 'json',
		async : false,
		success : function(data) {
			if(checkTokenError(data)){
			data_content = JSON.stringify(data);
			TableData = data.content.data;
			reviewTable(TableData);
			}
		},
        error:function(data){
            console.log(data)
            zxTips_short({
    			txt : "系统错误"
    			});
        }
	});
}
function reviewTable(TableData) {
	if(TableData){
	TableData.sort(DataSortUp);
	var TableList = "";

	for (var i = 0; i < TableData.length; i++) {
		TableList += '<tr data-id=' + TableData[i].id + '>';
		TableList += '<td><img alt="活动图片" class="PreviewPic" src="' + TableData[i].picturePath
				+ '"><div class="List-width list-name">' + TableData[i].theme
				+ '</div></td>';
		TableList += '<td>' + TableData[i].object + '</td>';
		TableList += '<td>';
		TableList += '<div><span class="startTime">开始</span><span class="time">'
				+ TableData[i].strDateStart + '</span></div>';
		TableList += '<div><span class="endTime">结束</span><span class="time">'
				+ TableData[i].strDateEnd + '</span></div>';
		TableList += '</td>';
		TableList += '<td><div class="List-width">' + TableData[i].addr
				+ '</div></td>';
		TableList += '<td class="control-button"><div class="Approval">批准</div><div class="NotApproval">不批准</div></td>';
		TableList += '<td><div class="preview">预览</div></td>';
		TableList += '</tr>';
	}
	$('#MyTable').find('tbody').html(TableList);
	}
}
function loadnotapproval() {
	$.ajax({
		type : "GET",
		url : UrlTable + "3",
		dataType : 'json',
		async : false,
		success : function(data) {
			if(checkTokenError(data)){
			data_content = JSON.stringify(data);
			TableData = data.content.data;
			notapprovalTable(TableData);
			}
		},
        error:function(data){
            console.log(data)
            zxTips_short({
    			txt : "系统错误"
    			});
        }
	});
}
function notapprovalTable(TableData) {
	TableData.sort(DataSortUp);
	var TableList = "";

	for (var i = 0; i < TableData.length; i++) {
		TableList += '<tr data-id=' + TableData[i].id + '>';
		TableList += '<td><img alt="活动图片" class="PreviewPic" src="' + TableData[i].picturePath
				+ '"><div class="List-width list-name">' + TableData[i].theme
				+ '</div></td>';
		TableList += '<td>' + TableData[i].object + '</td>';
		TableList += '<td>';
		TableList += '<div><span class="startTime">开始</span><span class="time">'
				+ TableData[i].strDateStart + '</span></div>';
		TableList += '<div><span class="endTime">结束</span><span class="time">'
				+ TableData[i].strDateEnd + '</span></div>';
		TableList += '</td>';
		TableList += '<td><div class="List-width">' + TableData[i].addr
				+ '</div></td>';
		TableList += '<td class="control-button"><div class="statu3 control-delete">删除</div></td>';
		TableList += '<td><div class="preview">预览</div></td>';
		TableList += '</tr>';
	}
	$('#MyTable').find('tbody').html(TableList);
}


//提示删除这一列
$("#MyTable .control-delete").click(function() {
	var myrow = $(this);
	var myname = myrow.parent().parent().find(".list-name").text();
	var myid = myrow.parent().parent().attr("data-id");
	var UrlDelete=DataURL + '/bank/activity/deleteActivityById';
	var DeleteData={"id":myid };
	zxTips_classic({
		Head : false,		// 是否存在头部
		ContentText : "是否将 " + myname + " <span class='statu2'>删除</span>？",
		// 提示内容
		Foot : true,		// 是否拥有底部
		FootCancel : true,		// 是否拥有底部取消按钮
		FootEnter : true,		// 是否拥有底部确认按钮
		clickEnter : function() {
			$.ajax({
				type : "POST",
				url : UrlDelete,
				dataType : 'json',
				data : DeleteData,// 传递给服务器的参数
				success : function(data) {
					zxTips_classicClose();
					if(checkTokenError(data)){
					zxTips_loadingend();
					zxTips_short({
						txt : data.description
					});
					table.row(myrow.parents('tr')).remove().draw();
					}
				},
		        error:function(data){
		            console.log(data)
		            zxTips_short({
		    			txt : "系统错误"
		    			});
		        }
			});
		}
	});
		});


// 列表ID排序
function DataSortUp(x, y) {
	return (x.id < y.id) ? 1 : -1

}
// 跳转预览页
$("#MyTable .preview")
		.click(
				function() {
					var myid = $(this).parent().parent().attr("data-id");
					window.location.href = HostURL
							+ '/activityManager/PreviewActivity?id=' + myid
							+ '&back='+page;
				});
// 引用列表插件
var table = $('#MyTable').DataTable({
	"searching" : false,
	"info" : false,
	"lengthChange" : false,
	"pageLength" : 5,
	"ordering" : false,
	language : {
		"sEmptyTable" : "没有数据！",
		"oPaginate" : {

			"sPrevious" : '<',
			"sNext" : '>',
		},
	}
});

// 批准操作
var UrlReview = DataURL + 'bank/activity/setCheckStatus';// 审核活动接口
$(".Approval").click(function() {
	var myrow = $(this);
	var myname = myrow.parent().parent().find(".list-name").text();
	var myid = myrow.parent().parent().attr("data-id");
	zxTips_classic({
		Head : false,
		// 是否存在头部
		ContentText : "是否<span class='statu1'>批准</span> " + myname + " 通过？",
		// 提示内容
		Foot : true,
		// 是否拥有底部
		FootCancel : true,
		// 是否拥有底部取消按钮
		FootEnter : true,
		// 是否拥有底部确认按钮
		clickEnter : function() {
			
			ReviewActivity(myid, myrow,2);
			zxTips_classicClose()
		}
	});
});



// 不批准操作
$(".NotApproval").click(function() {
	var myrow = $(this);
	var myname = myrow.parent().parent().find(".list-name").text();
	var myid = myrow.parent().parent().attr("data-id");
	zxTips_classic({
		Head : false,
		// 是否存在头部
		ContentText : "是否<span class='statu2'>不批准</span> " + myname + " 通过？",
		// 提示内容
		Foot : true,
		// 是否拥有底部
		FootCancel : true,
		// 是否拥有底部取消按钮
		FootEnter : true,
		// 是否拥有底部确认按钮
		clickEnter : function() {
			ReviewActivity(myid, myrow,3);
			zxTips_classicClose()
		}
	});
});
//审核操作
function ReviewActivity(ReviewId, ReviewRow,Status) {
	zxTips_loading({
		loadicon : true,// 是否添加加载图标（默认添加）
		loadtxt : "上传中"// 加载图片的路径
	});
	var controlData = {};
	controlData['id'] = ReviewId;
	controlData['checkStatus'] = Status;
	$.ajax({
		type : "POST",
		url : UrlReview,
		dataType : 'json',
		// contentType :"application/json;charset=UTF-8",
		data : controlData,// 传递给服务器的参数
		success : function(data) {
			if(checkTokenError(data)){
			zxTips_loadingend();
			zxTips_short({
				txt : data.description
			});
			table.row(ReviewRow.parents('tr')).remove().draw();
			}
		},
        error:function(data){
            console.log(data)
            zxTips_short({
    			txt : "系统错误"
    			});
        }
	});
}

// 切换页面
$(".maincontent-table .control-card .control-mode").click(
		function() {
			if (!$(this).hasClass("active")) {
				if ($(this).hasClass("ToReviewing")) {
					window.location.href = HostURL
							+ "/activityManager/reviewActivity";
				}
				if ($(this).hasClass("ToNotApproval")) {
					window.location.href = HostURL
							+ "/activityManager/NotApprovalActivity";
				}

			}
		});
//切换列表页
$('#ActivityTable tbody').bind("DOMNodeInserted", function () {
	$('.PreviewPic').unbind("click"); //移除click
	$(".PreviewPic").click(function() {
		zxTips_picpreview({
			picUrl:$(this).attr("src"),
			id:"myid",
			myclass:"myclass",
		});
		});
} );